package com.atguigu.mapper.impl;

import com.atguigu.mapper.UserMapper;
import com.atguigu.pojo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: liuaojun
 * @CreateTime: 2025-02-05
 * @Description:
 * @Version: 1.0
 */
@Repository
public class UserMapperImpl implements UserMapper {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void insertUser(Emp emp) {
        String sql="insert into t_emp(emp_name,emp_salary) values(?,?)";
        jdbcTemplate.update(sql,emp.getEmpName(),emp.getEmpSalary());

    }

    @Override
    public void updateUser(Emp emp) {
        String sql="update t_emp set emp_name=?, emp_salary=?  where emp_id=?";
        jdbcTemplate.update(sql,emp.getEmpName(),emp.getEmpSalary(),emp.getEmpId());
    }

    @Override
    public void deleteUser(Integer id) {
        String sql="delete from t_emp where emp_id=?";
        jdbcTemplate.update(sql,id);
    }

    @Override
    public Emp selectUser(Integer id) {
        String sql="select emp_id,emp_name,emp_salary from t_emp where emp_id=?";
        Emp emp = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Emp>(Emp.class), id);
        return emp;
    }

    @Override
    public List<Emp> selectAllUser() {
        String sql="select emp_id,emp_name,emp_salary from t_emp";
        List<Emp> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
        return query;
    }

}
